整合Mybatis和mysql pom.xml加入依赖 1 2 3 4 5 6 7 8 9 10 11 12 <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 1.3.2</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > </dependency >
修改application.yml 我们把application.properties文件改成application.yml(个人觉得yml可读性更好),加入以下配置:
1 2 3 4 5 6 7 8 9 spring: datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: root url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.tt.study.demo.entity
mapper-locations
表示mybatis的mapper文件的位置,type-aliases-package
表示mapper对应的实体类的包。
配置mybatis-generator-maven-plugin pom.xml添加以下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <plugin > <groupId > org.mybatis.generator</groupId > <artifactId > mybatis-generator-maven-plugin</artifactId > <version > 1.3.5</version > <dependencies > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.44</version > </dependency > </dependencies > <configuration > <verbose > true</verbose > <overwrite > true</overwrite > </configuration > </plugin >
resources下添加generatorConfig.xml文件,输入下面内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration > <context id ="Mysql" targetRuntime ="MyBatis3" > <commentGenerator > <property name ="suppressAllComments" value ="true" /> </commentGenerator > <jdbcConnection driverClass ="com.mysql.jdbc.Driver" connectionURL ="jdbc:mysql://localhost:3306/demo?useUnicode=true" userId ="root" password ="root" > </jdbcConnection > <javaTypeResolver > <property name ="forceBigDecimals" value ="false" /> </javaTypeResolver > <javaModelGenerator targetPackage ="com.tt.study.demo.entity" targetProject ="src/main/java" > <property name ="enableSubPackages" value ="true" /> <property name ="trimStrings" value ="false" /> </javaModelGenerator > <sqlMapGenerator targetPackage ="mapper" targetProject ="src/main/resources" > <property name ="enableSubPackages" value ="true" /> </sqlMapGenerator > <javaClientGenerator type ="XMLMAPPER" targetPackage ="com.tt.study.demo.mapper" targetProject ="src/main/java" > <property name ="enableSubPackages" value ="true" /> </javaClientGenerator > <table schema ="demo" tableName ="user" domainObjectName ="User" > <generatedKey column ="id" sqlStatement ="MySql" identity ="true" /> </table > </context > </generatorConfiguration >
具体每行配置的意思可以上MyBatis Generator 看一下。
配置完后,我们可以在IDEA的maven插件面板看到项目的Plugins
下多了一个mybatis-generator
,双击mybatis-generator:generate
可以运行generator。
测试一下 数据库新建一个user表:
1 2 3 4 5 6 CREATE TABLE user ( id bigint PRIMARY KEY AUTO_INCREMENT, username varchar(20) NOT NULL DEFAULT '' COMMENT '用户名', password varchar(128) NOT NULL DEFAULT '' COMMENT '密码', CONSTRAINT user_username_uindex UNIQUE user_username_uindex (username) ) ENGINE = InnoDB COMMENT '用户表';
双击maven插件面板的mybatis-generator:generate
运行generator。我们可以看到console输入下面相关信息:
1 2 3 4 5 6 7 8 9 [INFO] Introspecting table demo.user [INFO] Generating Example class for table user [INFO] Generating Record class for table user [INFO] Generating Mapper Interface for table user [INFO] Generating SQL Map for table user [INFO] Saving file UserMapper.xml [INFO] Saving file UserExample.java [INFO] Saving file User.java [INFO] Saving file UserMapper.java
再看我们的项目结构:
已经自动生成了实体类、mapper.xml、mapper.java和一个Example文件,关于这个Example可以自行上网查询一下,可以无需编写sql进行一些简单CRUD操作。
对了,我们看到DemoApplication下多了一个注解@MapperScan("com.tt.study.demo.mapper")
,这是声明扫描Mybatis的mapper类,没有这个注解,Spring无法注入mapper类。
接下来在service包下添加一个UserService:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Service public class UserService { @Autowired private UserMapper userMapper; public RespInfo<String> addUser (User user) { int count = userMapper.insert(user); if (count == 1 ) { return RespInfo.success("添加成功" ); } return RespInfo.error("添加失败" ); } }
HomeController下添加下面代码:
1 2 3 4 5 6 7 8 @Autowired private UserService userService;@RequestMapping ("/addUser" )@ResponseBody public Object addUser (User user) { return userService.addUser(user); }
好了,接下来我们页面请求一下:
ok,再看下数据库,是不是已经插入了一条新的数据。
pom.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 <?xml version="1.0" encoding="UTF-8"?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > com.tt.study</groupId > <artifactId > demo</artifactId > <version > 0.0.1-SNAPSHOT</version > <packaging > jar</packaging > <name > demo</name > <description > Demo project for Spring Boot</description > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.0.0.RELEASE</version > <relativePath /> </parent > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <project.reporting.outputEncoding > UTF-8</project.reporting.outputEncoding > <java.version > 1.8</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > 1.16.18</version > </dependency > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 1.3.2</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > <plugin > <groupId > org.mybatis.generator</groupId > <artifactId > mybatis-generator-maven-plugin</artifactId > <version > 1.3.5</version > <dependencies > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.44</version > </dependency > </dependencies > <configuration > <verbose > true</verbose > <overwrite > true</overwrite > </configuration > </plugin > </plugins > </build > </project >